$(function () {
    $("#jqGrid").Grid({
        url: '../devalarm/list',
        rownumWidth: 60,
        colModel: [
			{label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
			{label: '厂商', name: 'faName', index: 'FA_NAME', width: 100},
            {label: '厂商编码', name: 'faCode', index: 'FA_CODE', width: 100},
			{label: '设备类型', name: 'devType', index: 'DEV_TYPE', width: 100},
            {label: '报警码', name: 'code', index: 'CODE', width: 100},
            {label: '报警信息', name: 'msg', width: 100}
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        alarmModel: {devType: '', faName: '', faCode: '', code: '',msg:'',id:''}, // 报警Model
        ruleValidate: {
            devType: [
                {required: true, message: '设备类型必填', trigger: 'blur'}
            ],
            faCode: [
                {required: true, message: '厂商必选', trigger: 'blur'}
            ],
            code: [
                {required: true, message: '报警代码必填', trigger: 'blur'}
            ],
            msg: [
                {required: true, message: '报警信息必填', trigger: 'blur'}
            ]
        },
        q: {
            faName: '', // 厂家
            faCode: '', // 厂家编码
            code:'' // 报警代码
        }
    },
    methods: {
        loadFacType:function(){
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择厂家',
                content: '../sys/factorytype_choose.html?type=0',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getFacType();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.alarmModel.faCode = res.faCode;
                        vm.alarmModel.faName = res.faName;
                    }
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.alarmModel ={devType: '', faName: '', faCode: '', code: '',msg:'',id:''};
            $("#doSave").attr("disabled", false);
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            $("#doSave").attr("disabled", false);
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {

            $("#doSave").attr("disabled", true);

            let url = vm.alarmModel.id == (null || '') ? "../devalarm/save" : "../devalarm/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.alarmModel),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        getInfo: function(id){
            Ajax.request({
                url: "../devalarm/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.alarmModel = r.devAlarm;
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devalarm/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                faName: '',
                faCode:'',
                code:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (!res.err) {
                this.$Loading.finish();
                alert('导入成功', function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleError: function (err, file) {
            alert('导入失败');
        }
    }
});
